<?php

namespace app\controller\open;

use app\BaseController;
use app\model\StationBasic;
use app\model\SupplierStation;
use think\Response;

class Station extends BaseController
{
    public function lst(): Response
    {
        $param = $this->request->param();
        $supplier = $this->request->supplier;
        if (!empty($param['size']) && $param['size'] > 100) $param['size'] = 100;
        $data = $this->app->make(SupplierStation::class)->with(['station' => function ($query) {
            $query->with(['oil']);
        }])->when(!empty($param['station_id']), function ($query) use ($param) {
            $station_id = StationBasic::whereIn("number", explode(",", $param['station_id']))->column("id");
            $query->whereIn("station_id", $station_id);
        })->where(['supplier_id' => $supplier['id']])->paginate([
            'list_rows' => $param['size'] ?? 30,
            'page' => $param['page'] ?? 1,
        ])->toArray();
        if (!empty($data['data'])) {
            $data['data'] = array_map(function ($item) {
                $dat = $item['station'];
                $_92_rate = bcdiv($item['92_rate'], 100, 8);
                $_95_rate = bcdiv($item['95_rate'], 100, 8);
                $_98_rate = bcdiv($item['98_rate'], 100, 8);
                $_0_rate = bcdiv($item['0_rate'], 100, 8);
                $prices = ["sat_price", "mon_price", "tue_price", "wed_price", "thu_price", "fri_price", "sat_price"][date("w")];
//                $defPrice = $dat[$prices] > 0 ? $dat[$prices] : $dat['default_price'];
//                $unit = $dat['rate_unit'];
//                dd($defPrice,$item['station']);
                $dat['oil'] = array_map(function ($its) use ($_92_rate, $_95_rate, $_98_rate, $_0_rate, $prices) {
                    $def = $its[$prices] > 0 ? $its[$prices] : $its['default_price'];
                    $uni = $its['rate_unit'];
                    if ($uni == 2) {
                        $_def = bcsub(1, bcdiv($def, 100, 4), 4);
                        $discount = round($its['gun_price'] * $_def, 2);
                    } else {
                        $discount = round($its['gun_price'] - $def, 2);
                    }
                    $rate = match ($its['oil']) {
                        "92" => $_92_rate,
                        "95" => $_95_rate,
                        "98" => $_98_rate,
                        "0" => $_0_rate,
                        default => $_92_rate
                    };
                    $its['discount_price'] = $discount != $its['gun_price'] ? $discount : $its['gun_price'];
                    if ($rate > 0) {
                        $its['discount_price'] = round($discount * $rate, 2);
                    } elseif ($rate < 0) {
                        if ($uni == 2) {
                            $its['discount_price'] = round($its['gun_price'] * (1 - ($rate + bcdiv($def, 100, 4))), 2);
                        } else {
                            $rate = abs($rate);
                            $dis = round($its['gun_price'] * $rate, 2);
                            $its['discount_price'] = round($its['gun_price'] - $def - $dis, 2);
                        }
                    }
                    return [
                        "oil" => $its['oil'],
                        "oil_name" => $its['oil_name'],
                        "offic_price" => $its['offic_price'],
                        "gun_price" => $its['gun_price'],
                        "discount_price" => $its['discount_price'],
                        "guns" => $its['guns']
                    ];
                }, $dat['oil']);
                return [
                    "station_name" => $dat['name'],
                    "station_id" => $dat['number'],
                    "province" => $dat['province'],
                    "province_code" => $dat['province_code'],
                    "city" => $dat['city'],
                    "city_code" => $dat['city_code'],
                    "area" => $dat['area'],
                    "adcode" => $dat['adcode'],
                    "address" => $dat['address'],
                    "lng" => $dat['lng'],
                    "lat" => $dat['lat'],
                    "oil" => $dat['oil'],
                    "state" => $dat['state'],
                    "theme" => $dat['big_pic']
                ];
            }, $data['data']);
        }

        return $this->success(pageData($data));
    }
}
